<?php
namespace common\components\helpers;

use common\models\SmsLog;
use yii;

/**
 * 添加在components目录下的助手函数
 * Class TestHelper
 */
class PublicHelper
{


    /**
     * 设置过期时间和返回手机接收码
     * @param $length
     * @return null|string
     */
    public static function hidtel($phone)
    {
        //屏蔽电话号码中间的四位数字
        $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i', $phone); //固定电话
        if ($IsWhat == 1) {
            return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i', '$1****$2', $phone);

        } else {
            return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i', '$1****$2', $phone);
        }

    }

    /**
     *
     * 产生随机字符串，不长于32位
     */
    public static function getNonceStr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    public static function hidcard($str)
    {
        return substr_replace($str,str_repeat('*',6), 6, 8);
    }
    /**
     * 校验日期格式是否正确
     * @param string $date 日期
     * @param string $formats 需要检验的格式数组
     * @return boolean
     */
    public static function checkDateIsValid($date, $formats = array("Y-m-d", "Y/m/d")) {
        $unixTime = strtotime($date);
        if (!$unixTime) { //strtotime转换不对，日期格式显然不对。
            return false;
        }
        //校验日期的有效性，只要满足其中一个格式就OK
        foreach ($formats as $format) {
            if (date($format, $unixTime) == $date) {
                return true;
            }
        }
        return false;
    }
    
    
    /**
     * ---------------------------------------
     * 上传base64格式的图片
     * @param string $imgbase64 图片的base64编码
     * @return mixed
     * ---------------------------------------
     */
    public static function uploadImage($imgbase64){
    
    	if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgbase64, $result)){
    		$type = $result[2];
    		$type = $type == 'jpeg' ? 'jpg' : $type;
    		$fileName = time() . rand( 1 , 1000 ) . ".".$type;
    		/* 以年月创建目录 */
    		$dir = date('Ym', time());
    
    		if (!file_exists('uploads/exhibition/'.$dir)) {
    			mkdir('uploads/exhibition/'.$dir, 0777,true);
    		}
    		$fileName = $dir.'/'.$fileName;
    
    		if (file_put_contents('uploads/exhibition/'.$fileName, base64_decode(str_replace($result[1], '', $imgbase64)))){
    			return $fileName;
    		}
    
    	}
    	return false;
    }

    public static function sendCode($mobile,$message){
        //缓存
        $cache = \Yii::$app->cache;  $key = $mobile.date("Ymd");
        if($cache->get($key)) {
            $cache->set($key,intval($cache->get($key))+1);
        }else{
            $cache->set($key,1);
        }

        $smsModel = new SmsLog();
        $logData['SmsLog']['exhibition_id'] = 0;
        $logData['SmsLog']['name'] = $mobile;
        $logData['SmsLog']['mobile'] = $mobile;
        $logData['SmsLog']['msg'] = $message;
        $logData['SmsLog']['create_by'] = 0;

        //准备发送短信
        try {
            $sendRes = yii::$app->sm->send($mobile, $message);
            $resInfo = substr($sendRes->SendSmsTradeResult, 0, 1);
            if ($resInfo == 1 || $resInfo == 2) {
                //发送成功
                $logData['SmsLog']['status'] = 1;
            } else {
                $logData['SmsLog']['status'] = 0;
            }
        } catch (\Exception $e) {
            $logData['SmsLog']['status'] = 0;
        }
        //写入短信日志数据
        if ($smsModel->load($logData)) {
            $smsModel->save($logData);
        }
        //code 验证码
        return ['success' => true, 'message' => 'ok','code'=>Yii::$app->session->get('mcode')];

    }

    public static function smsSendNum($mobile,$sendNum)
    {
        $cache = \Yii::$app->cache;
        $key = $mobile.date("Ymd");
        return intval($cache->get($key))>=$sendNum?false:true;
    }

}